package io.renren.modules.carlunbo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.JacksonUtils;
import io.renren.common.utils.PageData;
import io.renren.modules.carinfo.entity.CarInfoEntity;
import io.renren.modules.carinfo.vo.CarInfoVO;
import io.renren.modules.carlunbo.dto.CarLunboDTO;
import io.renren.modules.carlunbo.vo.CarLunboVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;



import io.renren.modules.carlunbo.dao.CarLunboDao;
import io.renren.modules.carlunbo.entity.CarLunboEntity;
import io.renren.modules.carlunbo.service.CarLunboService;

import java.util.List;
import java.util.Map;

@Slf4j
@Service("carLunboService")
public class CarLunboServiceImpl extends ServiceImpl<CarLunboDao, CarLunboEntity> implements CarLunboService {

    @Override
    public PageData<CarLunboVO> getLunboList(Map<String, Object> params) {
        log.info("轮播图列表展示(分页---后台接口)");
        // 获取信息
        Integer page = Integer.valueOf((String) params.get("page"));
        Integer limit = Integer.valueOf((String) params.get("limit"));
        String remarks = (String) params.get("remarks");
        Object state = params.get("state");
        QueryWrapper<CarLunboEntity> wrapper = new QueryWrapper<CarLunboEntity>();
        if (StringUtils.isNotBlank(remarks)) {
            wrapper.like("remarks",remarks);
        }
        if (state != null){
            wrapper.eq("state",state);
        }
        wrapper.orderByDesc("sort");
        // 创建分页对象
        IPage<CarLunboEntity> iPage = new Page<CarLunboEntity>(page,limit);
        this.baseMapper.selectPage(iPage,wrapper);
        // 分页集合
        List<CarLunboEntity> records = iPage.getRecords();
        // 转换
        List<CarLunboVO> carLunboVOList = JacksonUtils.listObj2pojo(records, CarLunboVO.class);
        // 总数
        long total = iPage.getTotal();
        return new PageData<CarLunboVO>(carLunboVOList,total);
    }

    @Override
    public void saveCarLunbo(CarLunboDTO dto) {
        log.info("轮播图新增(后台接口)");
        CarLunboEntity carLunboEntity = JacksonUtils.obj2pojo(dto, CarLunboEntity.class);
        this.baseMapper.insert(carLunboEntity);
    }

    @Override
    public void updateCarLunbo(CarLunboDTO dto) {
        log.info("轮播图修改接口(后台接口)");
        CarLunboEntity carLunboEntity = JacksonUtils.obj2pojo(dto, CarLunboEntity.class);
        this.baseMapper.updateById(carLunboEntity);
    }

    @Override
    public void carLunboShelve(Long id, Integer state) {
        log.info("轮播图启用/禁用接口(后台接口)  '未选中：0 选中：1'");
        UpdateWrapper<CarLunboEntity> wrapper = new UpdateWrapper<CarLunboEntity>();
        wrapper.eq("id",id);
        CarLunboEntity carLunboEntity = new CarLunboEntity();
        carLunboEntity.setState(state);
        this.baseMapper.update(carLunboEntity,wrapper);
    }


}
